package com.lenovo.smarttraffic.ui.activity.stage4;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.ToastUtils;
import com.lenovo.smarttraffic.util.Utils;

public class QRCodePayActivity extends BaseActivity {

    private Toolbar toolbar;

    private LinearLayout desc_parent;
    private TextView type_tv;

    private LinearLayout gen_parent;
    private Spinner car_id_sp;
    private EditText money_et, time_et;
    private Button gen_btn;

    private LinearLayout qr_parent;
    private TextView qr_info;
    private ImageView image;

    private Handler handler = new Handler();

    private boolean can_refresh = false;

    private String qr_text;

    @Override
    protected int getLayout() {
        return R.layout.activity_qrcode_pay;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "二维码支付");

        toolbar = findViewById(R.id.toolbar);

        desc_parent = findViewById(R.id.desc_parent);
        type_tv = findViewById(R.id.type_tv);

        gen_parent = findViewById(R.id.gen_parent);
        car_id_sp = findViewById(R.id.car_id_sp);
        money_et = findViewById(R.id.money_et);
        time_et = findViewById(R.id.time_et);
        gen_btn = findViewById(R.id.gen_btn);

        qr_parent = findViewById(R.id.qr_parent);
        qr_info = findViewById(R.id.qr_info);
        image = findViewById(R.id.image);

        initData();
    }

    private void initData() {
        toolbar.setNavigationOnClickListener(v -> {
            if (qr_parent.getVisibility() == View.VISIBLE) {
                toggleView(false);
            } else finish();
        });

        gen_btn.setOnClickListener(v -> {
            int money = Utils.parseInt(money_et.getText().toString());
            int time = Utils.parseInt(time_et.getText().toString());
            if (money < 1 || time < 3) {
                ToastUtils.showToast(QRCodePayActivity.this, "请输入正确的数据");
            } else {
                toggleView(true);

                qr_text = "车辆编号=" + (car_id_sp.getSelectedItemPosition() + 1) + ",付费金额=" + money;
                qr_info.setText(qr_text);
                image.setImageBitmap(Utils.createQRCodeImage(qr_text + "\n" + System.currentTimeMillis(), 200, 200));

                refresh(time * 1000);
            }
        });

        image.setOnClickListener(v -> {
            if (qr_info.getVisibility() == View.VISIBLE) {
                qr_info.setVisibility(View.GONE);
                desc_parent.setVisibility(View.GONE);
            } else {
                qr_info.setVisibility(View.VISIBLE);
                desc_parent.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onBackPressedSupport() {
        if (qr_parent.getVisibility() == View.VISIBLE) {
            toggleView(false);
        } else super.onBackPressedSupport();
    }

    private void toggleView(boolean pay) {
        type_tv.setText(pay ? "向商家付款" : "生成付款二维码");

        qr_parent.setVisibility(pay ? View.VISIBLE : View.GONE);
        gen_parent.setVisibility(pay ? View.GONE : View.VISIBLE);

        if (!pay) {
            desc_parent.setVisibility(View.VISIBLE);
            qr_info.setVisibility(View.VISIBLE);
        }

        can_refresh = pay;
    }

    private void refresh(int delay) {
        if (can_refresh) {
            handler.postDelayed(() -> {
                runnable.run();
                refresh(delay);
            }, delay);
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (can_refresh) {
                image.setImageBitmap(Utils.createQRCodeImage(qr_text + "\n" + System.currentTimeMillis(), 200, 200));
            }
        }
    };

}
